package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class CartClearListener {
    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.CART_CLEAR_QUEUE,durable = "true"),
            exchange = @Exchange(name = MqConstants.TRADE_EXCHANGE_NAME,type = "topic"),
            key = MqConstants.ORDER_CREATE_KEY
    ))
    public void listenCartClear(Map<String,Object> map) {
        Number uId = (Number)map.get("userId");
        Long userId = uId.longValue();
        List<Number> iIds = (List<Number>)map.get("itemIds");
        List<Long> itemIds = iIds.stream().map(Number::longValue).collect(Collectors.toList());
        cartService.removeByItemIds(userId,itemIds);
    }
}
